package com.android.geotrack;

import java.util.ArrayList;

import com.android.geotrack.user.User;


import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

public class GeoTrack extends TabActivity
{
	public static User currentUser;
	
	public static final String TAG_GEO_TAB = "GeoActivity"; 
    public static final String TAG_TRACK_TAB = "TrackActivity"; 
     
    public static final int ID_GEO_TAB = 0; 
    public static final int ID_TRACK_TAB = 1;
    
    @Override
	public void onCreate(Bundle savedInstanceState)
	{
		final TabHost host = getTabHost(); 
		//requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
		super.onCreate(savedInstanceState);
		
		// get Last.fm username
		Intent i = this.getIntent();
		Bundle b = i.getExtras();
		this.currentUser = new User((String) b.get("user"));
		
		// ADD TABS
		
		host.addTab(host.newTabSpec(TAG_GEO_TAB)
				.setIndicator(TAG_GEO_TAB, getResources()
					.getDrawable(R.drawable.geo))
				.setContent(new Intent(this, GeoActivity.class)));
		
		setText(ID_GEO_TAB, "", new Float(10.0),host);
		
		host.addTab(host.newTabSpec(TAG_TRACK_TAB)
				.setIndicator(TAG_TRACK_TAB,getResources()
					.getDrawable(R.drawable.track))
				.setContent(new Intent(this, TrackActivity.class)));

		setText(ID_TRACK_TAB, "", new Float(10.0),host); 
		
		host.setOnTabChangedListener(new OnTabChangeListener()
		{
			public void onTabChanged(String tabId)
			{
				if(tabId.equals(TAG_GEO_TAB))
				{
					setIcon(host.getCurrentTab(), R.drawable.geo, host);
					setText(host.getCurrentTab(),"", new Float(10.0), host);
					setIcon(ID_TRACK_TAB, R.drawable.track,host);
					setText(ID_TRACK_TAB, "", new Float(10.0), host);
				}
				else if(tabId.equals(TAG_TRACK_TAB))
				{ 	
					setIcon(host.getCurrentTab(), R.drawable.track,host);
					setText(host.getCurrentTab(), "", new Float(10.0), host);
					setIcon(ID_GEO_TAB, R.drawable.geo,host);
					setText(ID_GEO_TAB, "", new Float(10.0), host);
				}
			} 
		}); 
	}
	
	private void setIcon(int whichTab, int resourceId,TabHost host)
	{ 
		ArrayList <View> views = host.getTabWidget().getChildAt(whichTab).getTouchables(); 
		RelativeLayout relLayout = (RelativeLayout)views.get(0);
		ImageView iv = (ImageView)relLayout.getChildAt(0);
		iv.setImageDrawable(getResources().getDrawable(resourceId)); 
	} 
  
	private void setText(int whichTab, String text, Float size, TabHost host)
	{
		ArrayList<View> views = host.getTabWidget().getChildAt(whichTab).getTouchables(); 
		RelativeLayout relLayout = (RelativeLayout)views.get(0); 
		TextView tv = (TextView)relLayout.getChildAt(1); 
		if(text != null){ 
			tv.setText(text); 
        } 
		tv.setTextSize(size);
	}
}